package cn.spy.threaddownload.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 异常处理器
 * @author sunpeiyu
 * @date 2023-04-10
 */
@RestControllerAdvice
public class CustomExtHandler {

    // 捕获全局异常,处理所有不可知的异常
    @ExceptionHandler(value=Exception.class)
    public Object handleException(Exception e, HttpServletRequest req){
        Map<String, Object> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        map.put("timestamp", calendar.getTime());
        map.put("status", 500);
        map.put("error", "Internal Server Error");
        map.put("message", e.getMessage());
        map.put("path", req.getRequestURI());
        return map;
    }
}
